package organisms;

import java.util.ArrayList;
import java.util.Iterator;

import processing.core.*;
import color.*;
import cluster.*;
import field.*;
import geom.Line;
import geom.PolarVector;
import geom.Vector;

public class LineMover extends Organism {
	Line line;
	float linePos;
	float speed;
	Cluster<Color> ancestry;
	ArrayList<TraceFiller> fillers;
	AngleField field;

	public LineMover(PApplet pa, AngleField field, Cluster<Color> ancestry,
			Vector pos, Line line) {
		super(pa, pos, field.getOffset(), field.getSize()
				.sub(field.getOffset()));
		this.line = line;
		linePos = 0;
		speed = 10;
		this.ancestry = ancestry;
		this.field = field;
		fillers = new ArrayList<TraceFiller>();
	}

	@Override
	public void draw() {
		Iterator<TraceFiller> tfit = fillers.iterator();
		while (tfit.hasNext()) {
			tfit.next().draw();
		}

	}

	public void drawTrace() {
		// Tracer t = (ColorTracer)line.getParent();
		// pa.fill(111);
		// pa.ellipse(pos.x, pos.y, 5, 5);
		// pa.noStroke();
		Iterator<TraceFiller> tfit = fillers.iterator();
		while (tfit.hasNext()) {
			tfit.next().drawTrace();
		}
	}

	@Override
	public void move() {
		Iterator<TraceFiller> tfit = fillers.iterator();
		while (tfit.hasNext()) {
			tfit.next().move();
		}
		linePos += speed;
		setPos(line.posAt(linePos));
		PolarVector newSpeed = pos.sub(prevPos).getPolar();
		newSpeed.setA(newSpeed.a + (float) Math.PI / 2f);
		fillers.add(new TraceFiller(pa, ancestry, field, this.pos, newSpeed
				.getCartesian()));
	}

	public void setPos(Vector pos) {

		prevPos = this.pos;
		this.pos = pos;

		getTrace().addPoint(pos.getCopy());

	}
}
